All files / web/src/app/api/arcade/rooms/[roomId]/kick route.ts

0% Statements 0/92
0% Branches 0/1
0% Functions 0/1
0% Lines 0/92

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93                                                                                                                                                                                         
import { NextResponse } from 'next/server'
import { kickUserFromRoom } from '@/lib/arcade/room-moderation'
import { getRoomMembers } from '@/lib/arcade/room-membership'
import { getRoomActivePlayers } from '@/lib/arcade/player-manager'
import { withAuth } from '@/lib/auth/withAuth'
import { getUserId } from '@/lib/viewer'
import { getSocketIO } from '@/lib/socket-io'

/**
 * POST /api/arcade/rooms/:roomId/kick
 * Kick a user from the room (host only)
 * Body:
 *   - userId: string
 */
export const POST = withAuth(async (request, { params }) => {
  try {
    const { roomId } = (await params) as { roomId: string }
    const userId = await getUserId()
    const body = await request.json()

    // Validate required fields
    if (!body.userId) {
      return NextResponse.json({ error: 'Missing required field: userId' }, { status: 400 })
    }

    // Check if user is the host
    const members = await getRoomMembers(roomId)
    const currentMember = members.find((m) => m.userId === userId)

    if (!currentMember) {
      return NextResponse.json({ error: 'You are not in this room' }, { status: 403 })
    }

    if (!currentMember.isCreator) {
      return NextResponse.json({ error: 'Only the host can kick users' }, { status: 403 })
    }

    // Can't kick yourself
    if (body.userId === userId) {
      return NextResponse.json({ error: 'Cannot kick yourself' }, { status: 400 })
    }

    // Verify the user to kick is in the room
    const targetUser = members.find((m) => m.userId === body.userId)
    if (!targetUser) {
      return NextResponse.json({ error: 'User is not in this room' }, { status: 404 })
    }

    // Kick the user
    await kickUserFromRoom(roomId, body.userId)

    // Broadcast updates via socket
    const io = await getSocketIO()
    if (io) {
      try {
        // Get updated member list
        const updatedMembers = await getRoomMembers(roomId)
        const memberPlayers = await getRoomActivePlayers(roomId)

        // Convert memberPlayers Map to object for JSON serialization
        const memberPlayersObj: Record<string, any[]> = {}
        for (const [uid, players] of memberPlayers.entries()) {
          memberPlayersObj[uid] = players
        }

        // Tell the kicked user they've been removed
        io.to(`user:${body.userId}`).emit('kicked-from-room', {
          roomId,
          kickedBy: currentMember.displayName,
        })

        // Notify everyone else in the room
        io.to(`room:${roomId}`).emit('member-left', {
          roomId,
          userId: body.userId,
          members: updatedMembers,
          memberPlayers: memberPlayersObj,
          reason: 'kicked',
        })

        console.log(`[Kick API] User ${body.userId} kicked from room ${roomId}`)
      } catch (socketError) {
        console.error('[Kick API] Failed to broadcast kick:', socketError)
      }
    }

    return NextResponse.json({ success: true }, { status: 200 })
  } catch (error: any) {
    console.error('Failed to kick user:', error)
    return NextResponse.json({ error: 'Failed to kick user' }, { status: 500 })
  }
})